package test;


import benchmark.Benchmark;
import data.IntegerDataSetGenerator;
import implementation.sort.AbstractSorter;
import implementation.sort.BubbleSort;
import implementation.sort.InsertionSort;
import implementation.sort.SelectionSort;

import java.util.Comparator;
import java.util.List;

public class SorterReport {

    public static Benchmark benchmark;

    public static void main(String[] args) {
        benchmark = new Benchmark();
        int n = 5000;

        BubbleSort bs = new BubbleSort();
        bs.addSorterListener(benchmark);
        print(bs, n);
        SelectionSort ss = new SelectionSort();
        ss.addSorterListener(benchmark);
        print(ss, n);


        InsertionSort is = new InsertionSort();
        is.addSorterListener(benchmark);
        print(is, n);
    }

    public static void print(AbstractSorter sorter, int n) {
        IntegerDataSetGenerator dataSetGenerator = new IntegerDataSetGenerator();
        Comparator comparator = dataSetGenerator.getComparator();
        System.out.println("\n"+sorter.getType() + " SORT (n = " + n + ") :");

        System.out.println("Best case:");
        printReport(sorter, dataSetGenerator.createAscending(n), comparator);

        System.out.println("Worst case:");
        printReport(sorter, dataSetGenerator.createDescending(n), comparator);
        System.out.println("Average case:");
        printReport(sorter, dataSetGenerator.createRandom(n), comparator);

    }

    private static void printReport(AbstractSorter sorter, List<Integer> list, Comparator comparator) {

        long t0 = System.currentTimeMillis();
        sorter.sort(comparator, list);
        long tf = System.currentTimeMillis();

        System.out.println("\t Time: " + (tf - t0));
        System.out.println("\t Comparisons: " + benchmark.getComparisons());
        System.out.println("\t Swaps: " + benchmark.getSwaps());
    }
}
